<template>
  <div class="layout-container">
    <div class="layout-container-form flex space-between">
      <div class="layout-container-form-handle">
        <el-button type="primary" :icon="Plus" @click="handleAdd">{{
            $t("message.common.add")
          }}
        </el-button>
      </div>
      <div class="layout-container-form-search">
        <el-input v-model="query.name" :placeholder="$t('message.common.searchTip')" @change="getTableData(false)">
        </el-input>
        <el-button type="primary" :icon="Search" class="search-btn" @click="getTableData(false)">{{
            $t("message.common.search")
          }}
        </el-button>
      </div>
    </div>
    <div class="layout-container-table">
      <span class="tag" v-for="item in tableData">
        <el-tag :hit="true" closable @close="handleDel([item])" @click="handleEdit(item)" :color="item.color"
                effect="dark" size="large">{{ item.name }}
        </el-tag>
      </span>
      <Layer :layer="layer" @getTableData="getTableData" v-if="layer.show"/>
    </div>
  </div>
</template>

<script lang="ts" setup>
import {inject, reactive, ref} from "vue";
import {Page} from "@/components/table/type";
import {del, listByUser} from "@/api/article/tag";
import Layer from "./layer.vue";
import {ElMessage, ElMessageBox} from "element-plus";
import type {LayerInterface} from "@/components/layer/index.vue";
import {Plus, Search} from "@element-plus/icons";
// 存储搜索用的数据
const query = reactive({
  name: "",
});
// 弹窗控制器
const layer: LayerInterface = reactive({
  show: false,
  title: "新增",
  showButton: true,
});
// 分页参数, 供table使用
const page: Page = reactive({
  index: 1,
  size: 20,
  total: 0,
});
const activeCategory: any = inject("active");
const loading = ref(true);
const tableData = ref([]);

// 获取表格数据
// params <init> Boolean ，默认为false，用于判断是否需要初始化分页
const getTableData = (init: boolean) => {
  loading.value = true;
  if (init) {
    page.index = 1;
  }
  let params = {
    category: activeCategory.value.id,
    ...page,
    ...query,
  };
  listByUser(params)
      .then((res) => {
        let data = res.data;
        tableData.value = data
        if (init) {
          page.total = Number(res.data.total);
        }

      })
      .catch((error) => {
        tableData.value = [];
        page.index = 1;
        page.total = 0;
      })
      .finally(() => {
        loading.value = false;
      });
};
// 删除功能
const handleDel = (data: object[]) => {
  ElMessageBox.confirm(
      '确认删除标签',
      '删除',
      {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      }
  )
      .then(() => {
        let params = [data
            .map((e: any) => {
              return e.tagId;
            })
            .join(","),
        ]
        del(params).then((res) => {
          ElMessage({
            type: "success",
            message: "删除成功",
          });
          getTableData(false);
        });
      })
      .catch(() => {

      })

};
// 新增弹窗功能
const handleAdd = () => {
  layer.title = "新增数据";
  layer.show = true;
  delete layer.row;
};
// 编辑弹窗功能
const handleEdit = (row: object) => {
  layer.title = "编辑数据";
  console.log(row)
  layer.row = row;
  layer.show = true;
};
// watch(activeCategory, (newVal) => {
//   getTableData(false);
// });
getTableData(false);
</script>

<style lang="scss" scoped>
.layout-container {
  width: calc(100% - 10px);
  height: 100%;
  margin: 0 0 0 10px;
  text-align: left
}

.tag {
  font-weight: bold;
  margin: 5px;
  cursor: pointer;
}
</style>